// 删除二叉树中的节点
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} key
 * @return {TreeNode}
 */
 var deleteNode = function(root, key) {
    //     if(root==null) return root;
    //     if(root.val==key) {
    //         if(root.right==null) return root.left;
    //         if(root.left==null) return root.right;
    //         if(root.right==null&&root.left==null) return null;
    //         if(root.right!==null && root.left!==null) {
    //             let target = get(root.left);
    //             root.val = target.val;
    //             root.left = deleteNode(root.left,target.val);
    //         }
            
    //     }
    //     if(root.val>key) {
    //         root.left=deleteNode(root.left,key)
    //     }
    //     if(root.val<key) {
    //        root.right= deleteNode(root.right,key)
    //     }
    //     return root;
    // function get(root) {
    //     if(root.right==null) return root;
    //     return get(root.right);
    // }
    if (root === null)
            return root;
        if (root.val === key) {
            if (!root.left)
                return root.right;
            else if (!root.right)
                return root.left;
            else {
                let cur = root.right;
                while (cur.left) {
                    cur = cur.left;
                }
                cur.left = root.left;
                root = root.right;
                delete root;
                return root;
            }
        }
        if (root.val > key)
            root.left = deleteNode(root.left, key);
        if (root.val < key)
            root.right = deleteNode(root.right, key);
        return root;
    };